package org.mybatis;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.domian.User;
import org.mybatis.mapper.UserMapper;

import java.io.IOException;
import java.io.Reader;

/***
 *
 *
 * @author liuhc
 * @date 2019-05-02 
 */
public class MybatisHelloWorld {

    public static void main(String[] args) {

        String resource = "org/mybatis/Configuration.xml";

        Reader reader;

        try {
            reader = Resources.getResourceAsReader(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);

            SqlSession sqlSession = sqlSessionFactory.openSession();

            try {
                User user = sqlSession.selectOne("getUser", 4);
                System.out.printf("user:", user);

                UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
                User user1 = userMapper.getUser(4);
                System.out.printf("user1", user1);
            } finally {
                sqlSession.close();
            }
        } catch (IOException e) {

        }

    }
}
